<!--
 * @Author: Dorange.Wang
 * @Date: 2021-03-31 16:04:51
 * @LastEditors: wangming
 * @LastEditTime: 2022-05-25 15:26:52
 * @Description: file content
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="580px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>全局配置</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>
    <el-form
      class="pl30 pr30"
      :model="layout"
      inline
      ref="Form"
      label-width="120px"
    >
      <div><el-form-item label="页面初始化钩子"> ready </el-form-item></div>
      <div><el-form-item label="页面销毁后钩子"> destroyed </el-form-item></div>
      <div><el-form-item label="触发缓存钩子"> activated </el-form-item></div>
      <el-form-item label="页面缓存">
        <el-switch v-model="keepAlive"> </el-switch>
      </el-form-item>
      <el-form-item label="基础布局">
        <el-radio-group v-model="layout.type" @change="changeType">
          <el-radio-button
            :key="index"
            :label="item.value"
            v-for="(item, index) in LAYOUT_CONFIG"
          >
            <i class="iconfont f28" :class="item.icon"></i>
          </el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="样式" v-if="typeList.length > 0">
        <el-radio-group v-model="layout.styleType">
          <el-radio-button
            :label="item.type"
            v-for="(item, index) in typeList"
            :key="index"
          >
            <i class="iconfont f28" :class="item.icon"></i>
          </el-radio-button>
        </el-radio-group>
      </el-form-item>
      <div v-if="layout.type == 'lrcontainer' && layout.styleType != 'center'">
        <el-form-item label="宽度可调节">
          <el-switch v-model="layout.data.Resize"> </el-switch>
        </el-form-item>
        <el-form-item label="初始宽度(px)">
          <el-input v-model="layout.data.width" isNumber></el-input>
        </el-form-item>
      </div>
      <div v-if="layout.type == 'tbcontainer' && layout.styleType != 'middle'">
        <el-form-item label="高度可调节">
          <el-switch v-model="layout.data.Resize"> </el-switch>
        </el-form-item>
        <el-form-item label="初始高度(px)">
          <el-input v-model="layout.data.width" isNumber></el-input>
        </el-form-item>
      </div>
      <div v-if="layout.type == 'ltbcontainer'">
        <el-form-item label="侧栏可调节">
          <el-switch v-model="layout.data.AsideResize"> </el-switch>
        </el-form-item>
        <el-form-item label="侧栏初始宽度(px)">
          <el-input v-model="layout.data.Asidewidth" isNumber></el-input>
        </el-form-item>
        <el-form-item label="底栏可调节">
          <el-switch v-model="layout.data.FooterResize"> </el-switch>
        </el-form-item>
        <el-form-item label="底栏初始高度(px)">
          <el-input v-model="layout.data.Footerwidth" isNumber></el-input>
        </el-form-item>
      </div>
    </el-form>
    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { LAYOUT_CONFIG } from "./config.js";
import { cloneDeep } from "lodash";
import { Events } from "@/utils/Dynamic.js";
export default {
  data() {
    return {
      LAYOUT_CONFIG,
      showVisible: false,
      layout: {},
      callback: () => {},
      typeList: [],
      readyFun: "",
      keepAlive: false,
    };
  },
  methods: {
    changeType(value) {
      let lay = this.LAYOUT_CONFIG.find((i) => i.value == value) || {};
      this.typeList = lay.typeList || [];
      this.layout.data = this.typeList[0] || {};
      this.layout.styleType = this.layout.data.type || "";
    },
    openForm({ layout, readyFun, js, keepAlive }) {
      this.keepAlive = keepAlive || false;
      this.js = js;
      this.readyFun = readyFun;
      this.layout = cloneDeep(layout);
      this.typeList =
        this.LAYOUT_CONFIG.find((i) => i.value == layout.type)?.typeList || [];
      this.showVisible = true;
      this.$nextTick(() => {
        this.$refs.Form.clearValidate();
      });
      return {
        then: (callback) => {
          if (typeof callback === "function") {
            this.callback = callback;
          }
        },
      };
    },
    async submit() {
      try {
        this.showVisible = false;
        if (
          this.layout.styleType === "middle" ||
          this.layout.styleType === "center"
        ) {
          this.layout.data.Resize = false;
        }
        this.js = Events.addFunctionJSON(this.js, this.readyFun);
        this.callback({
          layout: this.layout,
          readyFun: this.readyFun,
          js: this.js,
          keepAlive: this.keepAlive,
        });
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
